import axios from 'axios'
import NproGress from 'nprogress'
import { Message } from 'element-ui'
import whiteList from './whiteList'
import 'nprogress/nprogress.css'
// Nprogress  加载中的状态

let http = axios.create()

http.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1"

// 请求拦截
http.interceptors.request.use((config) => {
    let token = sessionStorage.getItem('token') ? sessionStorage.getItem('token') : ''
    config.headers.Authorization = token
    NproGress.start()
    return config
})


// 响应拦截
http.interceptors.response.use((data) => {
    // 请求的url
    let url = data.config.url
    // 请求的方式
    let method = data.config.method
    
    NproGress.done()
    let flag = whiteList.some(item => item.method === method && url.includes(item.url))
    if (flag) {
        const { status, msg } = data.data.meta
        switch (status) {
            case 200:
            case 201:
                Message.success(msg)
                break
            case 400:
                Message.warning(msg)
                break
        }
    }

    return data.data.data
})

export default http
